package com.haotunet.app.goldtreasure.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.goldplusgold.imageloader.lib.GlideRoundTransform;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.activity.MerchantDetailActivity;
import com.haotunet.app.goldtreasure.model.StoreInfo;

import java.util.List;

public class MerchantAdapter extends HolderAdapter<StoreInfo, MerchantAdapter.ViewHolder> {

    public MerchantAdapter(Context context, List<StoreInfo> mlist) {
        super(context, mlist);
    }

    public static class ViewHolder {
        TextView tvDistance;
        TextView tvShopName;
        TextView tvShopAddress;
        TextView tvDeposit;
        TextView tvExtract;
        TextView tvExchange;
        ImageView shopImage;
        ImageView ivSupport;
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, StoreInfo t, int position) {
        return inflate(R.layout.item_merchant);
    }

    @Override
    public ViewHolder buildHolder(View convertView, StoreInfo t, int position) {
        ViewHolder holder = new ViewHolder();
        holder.tvDistance = (TextView) convertView.findViewById(R.id.distance);
        holder.tvShopAddress = (TextView) convertView.findViewById(R.id.shop_address);
        holder.shopImage = (ImageView) convertView.findViewById(R.id.iv_merchant_iamge);
        holder.tvShopName = (TextView) convertView.findViewById(R.id.shop_name);
        holder.tvDeposit = (TextView) convertView.findViewById(R.id.txt_deposit);
        holder.tvExtract = (TextView) convertView.findViewById(R.id.txt_extract);
        holder.tvExchange = (TextView) convertView.findViewById(R.id.txt_exchange);
        holder.ivSupport = (ImageView) convertView.findViewById(R.id.support_icon);
        return holder;
    }

    @Override
    public void bindViewDatas(View convertView, ViewHolder holder, final StoreInfo storeInfo, int position) {
        Glide.with(context).load(storeInfo.getThumbnail().trim()).placeholder(R.drawable.placeholder_shop_small).error(R.drawable.placeholder_shop_small).transform(new GlideRoundTransform(context, 4)).into(holder.shopImage);
        holder.tvShopAddress.setText(storeInfo.getAddress());
        holder.tvShopName.setText(storeInfo.getShopName());
        holder.tvDistance.setText(storeInfo.getDistance());
        if ("1".equals(storeInfo.getIsDepositSupport())) {
            holder.tvDeposit.setVisibility(View.VISIBLE);
        } else {
            holder.tvDeposit.setVisibility(View.GONE);
        }
        if ("1".equals(storeInfo.getIsExtractSupport())) {
            holder.tvExtract.setVisibility(View.VISIBLE);
        } else {
            holder.tvExtract.setVisibility(View.GONE);
        }
        if ("1".equals(storeInfo.getIsExchangeSupport())) {
            holder.tvExchange.setVisibility(View.VISIBLE);
        } else {
            holder.tvExchange.setVisibility(View.GONE);
        }
        if ("0".equals(storeInfo.getIsDepositSupport()) && "0".equals(storeInfo.getIsExtractSupport())
                && "0".equals(storeInfo.getIsExtractSupport())) {
            holder.ivSupport.setVisibility(View.GONE);
        } else {
            holder.ivSupport.setVisibility(View.VISIBLE);
        }
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MerchantDetailActivity.class);

                intent.putExtra("shopId", storeInfo.getShopId());
                context.startActivity(intent);
            }
        });

    }
}
